#include "debug.h"
#include "../DDIC/AllDDIC.h"
#include "usbd_cdc_core.h"

/********************************************************************************************

********************************************************************************************/
//567QHD

void GM920_Enable_Flash_Control_and_SPI_Flash_En(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u16 tmp=0;
	u8 buffer1[3];	
	u8 RM69351_Enable_Flash_Control[24] = {0xfe,0xd2,0x01,0xcf,0xfe,0x40,0x4d,0x20,0xfe,0xd2,0x05,0x18,0xfe,0x20,0xf3,0x08,0xf3,0x00,0xf3,0x04,0xf3,0x02,0xfe,0xe0};
	u8 RM69351_Enable_Flash_Read_REG[3] = {0x6d,0x6e,0x6f};	
	
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(15);
	for(i=0;i<sizeof(RM69351_Enable_Flash_Control);i++)
	{
	buffer1[0] = 0x02;
	buffer1[1] = RM69351_Enable_Flash_Control[i];
	buffer1[2] = RM69351_Enable_Flash_Control[i+1];
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	delay_ms(15);
	i+=1;
	}
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080); 
	SSD2828_W_Reg(SigMode,channel,0xBC,0x0001); 
	for( i = 0; i< 3;i++)				
	{	
		SSD2828_W_Reg(SigMode,channel,0xC1,0x0001); //return package						   
		buffer1[0] = 0x01;
		buffer1[1] = RM69351_Enable_Flash_Read_REG[i];
		SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(6);
		buffer[4+i]=SSD2828_R_Reg(SigMode,channel,0xFF);		
	}	
	delay_ms(5);
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //·μ?? ?áè?μ? Gammaêy?Y  ￡ook  
}
void GM920_Inter_Demura_Ram(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[3];
	u8 RM69351_Inter_Demura_Ram_Cmd_Table[22]={0xfe,0x40,0x0e,0x80,0xfe,0xd2,0x18,0x55,0xfe,0xd0,0x03,0x09,0x4d,0x01,0x78,0x01,0xfe,0x40,0x10,0x82,0xfe,0x00};		
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(15);

	for(i=0;i<sizeof(RM69351_Inter_Demura_Ram_Cmd_Table);i++)
	{
		buffer1[0] = 0x02;
		buffer1[1] = RM69351_Inter_Demura_Ram_Cmd_Table[i];
		buffer1[2] = RM69351_Inter_Demura_Ram_Cmd_Table[i+1];
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(15);
		i+=1;
	}		
	buffer[0] = 0x2F;
	buffer[1] = 0x01;		
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);        																					
}
                                    
void GM920_Exit_Demura_Ram(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{

}
                               
void GM920_Flash_Write_Demura(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
		u32 error_times=0;
		u8 buffer1[3];
		u8 RM69351_Flash_Write_Demura_data_Table[24]={0xfe,0x20,0xf2,0x04,0xfe,0xd2,0x18,0x55,0x12,0x00,0x13,0x00,0x14,0x00,0x15,0x17,0x16,0xff,0x17,0xff,0xfe,0x20,0xf2,0x10};		
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		delay_ms(15);	
//----------------------------------------------------------------------RM69350			
		buffer[0] = 0x2F;
		buffer[1] = 0x03;		
		buffer[4] = 0x08;	
		
		for(i=0;i<sizeof(RM69351_Flash_Write_Demura_data_Table);i++)
		{
			buffer1[0] = 0x02;
			buffer1[1] = RM69351_Flash_Write_Demura_data_Table[i];
			buffer1[2] = RM69351_Flash_Write_Demura_data_Table[i+1];
			SSD2828_W_Array(SigMode,channel,buffer1,0);
			delay_ms(100);
			i+=1;
		}	
		delay_ms(1000);
		
		buffer1[0] = 0x02;
		buffer1[1] = 0xfe;
		buffer1[2] = 0xe0;
		SSD2828_W_Array(SigMode,channel,buffer1,0);			
		delay_ms(5);
	//	SSD2828_W_Reg(SigMode,channel,0xB7,((HS_B7_Data|0x0010)&0XFFFD)|0x0080);  //bit7 1:?á2ù×÷ bit4 1￡oHS CLOCK Disable
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080); 
		delay_ms(15);
		SSD2828_W_Reg(SigMode,channel,0xC1,0x0002); //return package  
		do
		{                        
			buffer1[0] = 0x01;
			buffer1[1] = 0x6B;
			SSD2828_W_Array(SigMode,channel,buffer1,0); 
			delay_ms(20);
			buffer[4]=SSD2828_R_Reg(SigMode,channel,0xFF);   
			delay_ms(50);
			if((++error_times)>300)//15sec timeout error
			{
				buffer[4] = 0x00;	
				goto error_tips0;
			}				
		}while(buffer[4]!=0x08);	

	error_tips0:
		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
		STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);		
}                       

void GM920_Flash_Write_Demura2(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u32 error_time=0;
	u8 buffer1[3];
	USB_Rx_Demura_Total_Num+=0x180000;
	u8 RM69351_Flash_Write_Demura_data_Table2[24]={0xfe,0x20,0xf2,0x04,0xfe,0xd2,0x18,0x55,0x12,0x18,0x13,0x00,0x14,0x00,0x15,0x26,0x16,0xe3,0x17,0x0a,0xfe,0x20,0xf2,0x10}; //7d		
		
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	//SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFfd);		
	delay_ms(15);		
		
	buffer[0] = 0x2F;
	buffer[1] = 0x0A;		
	buffer[4] = 0x08;	
	for(i=0;i<sizeof(RM69351_Flash_Write_Demura_data_Table2);i++)
	{
		buffer1[0] = 0x02;
		buffer1[1] = RM69351_Flash_Write_Demura_data_Table2[i];
		buffer1[2] = RM69351_Flash_Write_Demura_data_Table2[i+1];
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(100);
		i+=1;
	}	
	//------------------------------------------------------------- 
	delay_ms(1000);
	
	buffer1[0] = 0x02;
	buffer1[1] = 0xfe;
	buffer1[2] = 0xe0;
	SSD2828_W_Array(SigMode,channel,buffer1,0);			
	delay_ms(50);		
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);
		delay_ms(15);		
	//SSD2828_W_Reg(SigMode,channel,0xB7,((HS_B7_Data|0x0010)&0XFFFD)|0x0080);  //bit7 1:?á2ù×÷ bit4 1￡oHS CLOCK Disable
	SSD2828_W_Reg(SigMode,channel,0xBC,0x0001); 
	SSD2828_W_Reg(SigMode,channel,0xC1,0x0002); //return package  
	
	do
	{						 
		buffer1[0] = 0x01;
		buffer1[1] = 0x6B;
		SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(20);
		buffer[4]=SSD2828_R_Reg(SigMode,channel,0xFF);	 
		delay_ms(50);
		if((++error_time)>300)//15sec timeout error
		{
			buffer[4] = 0x00;	
			goto error_tips1;
		}
	}while(buffer[4]!=0x08);	
	
error_tips1:
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);
	
}


void GM920_Flash_Check_Demura(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u16 tmp=0;
	u8 buffer1[3];
	//u8 RM69351_Flash_Check_Read_REG[2]={0xf8,0xf9};
	//u8 RM69351_Flash_Check_Demura_data_Table[30]={0xfe,0xd2,0x3d,0xd4,0xa4,0x20,0x0d,0x04,0xfe,0x20,0x09,0x00,0x31,0x00,0x32,0x00,0x33,0x00,0x34,0x26,0x35,0xe3,0x36,0x0A,0xfe,0xd2,0xa0,0x21,0x06,0x23};	
	u8 RM69351_Flash_Check_Read_REG[2]={0x69,0x6A};
	u8 RM69351_Flash_Check_Demura_data_Table[32]={0xfe,0xd2,0x06,0x03,0xfe,0x20,0xf3,0x08,0xf3,0x00,0xf3,0x04,0xf3,0x02,0xfe,0x20,0xF2,0x04,0xFE,0xD2,0x12,0x00,0x13,0x00,0x14,0x00,0x15,0x26,0x16,0xE3,0x17,0x08};		
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(15);

	for(i=0;i<sizeof(RM69351_Flash_Check_Demura_data_Table);i++)
	{
		buffer1[0] = 0x02;
		buffer1[1] = RM69351_Flash_Check_Demura_data_Table[i];
		buffer1[2] = RM69351_Flash_Check_Demura_data_Table[i+1];
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(6);
		i+=1;
	}	
	delay_ms(30);			
	buffer1[0] = 0x02;
	buffer1[1] = 0xfe;
	buffer1[2] = 0x20;
	SSD2828_W_Array(SigMode,channel,buffer1,0);			
	buffer1[0] = 0x02;
	buffer1[1] = 0xf2;
	buffer1[2] = 0x01;
	SSD2828_W_Array(SigMode,channel,buffer1,0);			
	delay_ms(1200);
	
	buffer1[0] = 0x02;
	buffer1[1] = 0xfe;
	buffer1[2] = 0xE0;
	SSD2828_W_Array(SigMode,channel,buffer1,0);			
	delay_ms(15);		
	
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080); 
	SSD2828_W_Reg(SigMode,channel,0xBC,0x0001); 	
	for(i=0;i<sizeof(RM69351_Flash_Check_Read_REG);i++) 			
	{	
		SSD2828_W_Reg(SigMode,channel,0xC1,0x0001); //return package						   
		buffer1[0] = 0x01;
		buffer1[1] = RM69351_Flash_Check_Read_REG[i];
		SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(6);
		buffer[4+i]=SSD2828_R_Reg(SigMode,channel,0xFF);		
	}			
	
	delay_ms(5);
	buffer[0] = 0x2F;
	buffer[1] = 0x04;					
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //·μ?? ?áè?μ? Gammaêy?Y  ￡ook  	
}
                                  
void GM920_Flash_Erase_Demura(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[3];
	u8 RM69351_Flash_Erase_Demura_data_Table[10]={0xfe,0xd2,0x18,0x55,0xfe,0x20,0xf2,0x04,0xf3,0x01};	
	u8 RM69351_Read_REG=0x6B;	
	
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(15);
		
	for(i=0;i<sizeof(RM69351_Flash_Erase_Demura_data_Table);i++)
	{
		buffer1[0] = 0x02;
		buffer1[1] = RM69351_Flash_Erase_Demura_data_Table[i];
		buffer1[2] = RM69351_Flash_Erase_Demura_data_Table[i+1];
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(6);
		i+=1;
	}
	delay_ms(5000);	
	buffer1[0] = 0x02;
	buffer1[1] = 0xfe;
	buffer1[2] = 0xe0;
	SSD2828_W_Array(SigMode,channel,buffer1,0);			
	delay_ms(15);		
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080); 
	SSD2828_W_Reg(SigMode,channel,0xBC,0x0001); 
	SSD2828_W_Reg(SigMode,channel,0xC1,0x0002); //return package  
	do
	{						 
		buffer1[0] = 0x01;
		buffer1[1] = RM69351_Read_REG;
		SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(6);
		buffer[4]=SSD2828_R_Reg(SigMode,channel,0xFF);		
	}while((buffer[4]&0x08)!=0x08);
	delay_ms(5);
	buffer[0] = 0x2F;
	buffer[1] = 0x05;		
	buffer[4] = 0x08;		
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //êy?Y3¤?è-crcá?×??ú			
}

void GM920_Flash_Demura_OTP(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	;
}

void GM920_Demura_Function_ON(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[7];
	u8 RM69351_Demura_Function_ON_Table[8] = {0xfe, 0xd2, 0x0e, 0xc0, 0x0f, 0x7f, 0xAA, 0x01};
	
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(15);	

	for(i=0;i<sizeof(RM69351_Demura_Function_ON_Table);i++)
	{
		buffer1[0] = 0x02;
		buffer1[1] = RM69351_Demura_Function_ON_Table[i];
		buffer1[2] = RM69351_Demura_Function_ON_Table[i+1];
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(50);
		i+=1;
	}

	delay_ms(5);
	buffer[0] = 0x2F;
	buffer[1] = 0x07;		
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //êy?Y3¤?è-crcá?×??ú
}

void GM920_Demura_Function_OFF(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[7];
	u8 RM69351_Demura_Function_OFF_Table[8] = {0xfe,0xd2,0x0e,0xc0,0x0f,0x7f,0xAA,0x81};
	
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(15);
	for(i=0;i<sizeof(RM69351_Demura_Function_OFF_Table);i++)
	{
		buffer1[0] = 0x02;
		buffer1[1] = RM69351_Demura_Function_OFF_Table[i];
		buffer1[2] = RM69351_Demura_Function_OFF_Table[i+1];
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(50);
		i+=1;
	}
	delay_ms(5);
	buffer[0] = 0x2F;  //Demura_type
	buffer[1] = 0x08;				
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //·μ?? ?áè?μ? Gammaêy?Y  ￡ook  
}

void ProcessForDmuR14( USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{	
	switch(USB_Rx_Buffer[1])
	{
			case 0x01:                                      //写寄存器
					GM920_Inter_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x02:                                      //读寄存器
					GM920_Exit_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;						 
			case 0x03:                                      //读寄存器
					GM920_Flash_Write_Demura(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x04:                                      //读寄存器
					GM920_Flash_Check_Demura(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;												
			case 0x05:                                      //读寄存器
					GM920_Flash_Erase_Demura(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;
			case 0x06:                                      //check flash ID
					GM920_Enable_Flash_Control_and_SPI_Flash_En(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x07:                                      //check flash ID
					GM920_Demura_Function_ON(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x08:                                      //check flash ID
					GM920_Demura_Function_OFF(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;													
			case 0x0A:
					GM920_Flash_Write_Demura2(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;
			default:
				break;												
	}
}
//********************************************************************************************




